
import * as problem_pb from '../protos/problem_pb.js'
import * as blog_pb from '../protos/blog_pb.js'
import * as user_pb from '../protos/user_pb.js'
import * as home_pb from '../protos/home_pb.js'
import * as chat_pb from '../protos/chat_pb.js'

const proto = {
  problem: problem_pb,
  blog: blog_pb,
  user: user_pb,
  home: home_pb,
  chat: chat_pb
}

// 直接导出protobuf类型
export const problem = {
  Problem: proto.problem.Problem,
  ProblemDetail: proto.problem.ProblemDetail,
  TestCase: proto.problem.TestCase,
  ProblemListRequest: proto.problem.ProblemListRequest,
  ProblemListResponse: proto.problem.ProblemListResponse,
  ProblemDetailRequest: proto.problem.ProblemDetailRequest,
  ProblemDetailResponse: proto.problem.ProblemDetailResponse,
  DiscussionListRequest: proto.problem.DiscussionListRequest,
  DiscussionListResponse: proto.problem.DiscussionListResponse,
  SubmissionListRequest: proto.problem.SubmissionListRequest,
  SubmissionListResponse: proto.problem.SubmissionListResponse,
  SubmitRequest: proto.problem.SubmitRequest,
  SubmitResult: proto.problem.SubmitResult,
  SubmissionDetailRequest: proto.problem.SubmissionDetailRequest,
  SubmitResponse: proto.problem.SubmitResponse,
  UserProblemListRequest: proto.problem.UserProblemListRequest,
  UserProblemListResponse: proto.problem.UserProblemListResponse,
};

export const blog = {
  BlogPreview: proto.blog.BlogPreview,
  BlogDetail: proto.blog.BlogDetail,
  Comment: proto.blog.Comment,
  BlogListRequest: proto.blog.BlogListRequest,
  UserBlogListRequest: proto.blog.UserBlogListRequest,
  BlogListResponse: proto.blog.BlogListResponse,
  BlogDetailRequest: proto.blog.BlogDetailRequest,
  BlogDetailResponse: proto.blog.BlogDetailResponse,
  BlogCommentRequest: proto.blog.BlogCommentRequest,
  BlogCommentResponse: proto.blog.BlogCommentResponse,
  LikeRequest: proto.blog.LikeRequest,
  LikeResponse: proto.blog.LikeResponse,
  CommentRequest: proto.blog.CommentRequest,
  CommentResponse: proto.blog.CommentResponse,
  DeleteBlogRequest: proto.blog.DeleteBlogRequest,
  DeleteBlogResponse: proto.blog.DeleteBlogResponse,
  CreateBlogRequest: proto.blog.CreateBlogRequest,
  CreateBlogResponse: proto.blog.CreateBlogResponse,
  EditBlogRequest: proto.blog.EditBlogRequest,
  EditBlogResponse: proto.blog.EditBlogResponse,
  LikedBlogListRequest: proto.blog.LikedBlogListRequest,
  LikedBlogListResponse: proto.blog.LikedBlogListResponse,
  DeleteLikeBlogRequest: proto.blog.DeleteLikeBlogRequest,
  DeleteLikeBlogResponse: proto.blog.DeleteLikeBlogResponse
};

export const home = {
  HomeDataRequest: proto.home.HomeDataRequest,
  HomeDataResponse: proto.home.HomeDataResponse,
  SearchResponse: proto.home.SearchResponse,
  SearchRequest: proto.home.SearchRequest
};

export const user = {
  UserStats: proto.user.UserStats,
  UserStatsRequest: proto.user.UserStatsRequest,
  UserStatsResponse: proto.user.UserStatsResponse,
  VerificationCodeRequest: proto.user.VerificationCodeRequest,
  VerificationCodeResponse: proto.user.VerificationCodeResponse,
  RegisterRequest: proto.user.RegisterRequest,
  RegisterResponse: proto.user.RegisterResponse,
  LoginRequest: proto.user.LoginRequest,
  CodeLoginRequest: proto.user.CodeLoginRequest,
  LoginResponse: proto.user.LoginResponse,
  LogoutResponse: proto.user.LogoutResponse,
  LogoutRequest: proto.user.LogoutRequest,
  UserStatsSummary: proto.user.UserStatsSummary,
  UserStatsSummaryRequest: proto.user.UserStatsSummaryRequest,
  UserStatsSummaryResponse: proto.user.UserStatsSummaryResponse,
  UpdateUsernameRequest: proto.user.UpdateUsernameRequest,
  UpdateUsernameResponse: proto.user.UpdateUsernameResponse,
  UpdateAvatarRequest: proto.user.UpdateAvatarRequest,
  UpdateAvatarResponse: proto.user.UpdateAvatarResponse,
  ChangePasswordRequest: proto.user.ChangePasswordRequest,
  ChangePasswordResponse: proto.user.ChangePasswordResponse,
  ChangePasswordByEmailRequest: proto.user.ChangePasswordByEmailRequest,
  GetFollowStatusRequest: proto.user.GetFollowStatusRequest,
  GetFollowStatusResponse: proto.user.GetFollowStatusResponse,
  FollowRequest: proto.user.FollowRequest,
  FollowResponse: proto.user.FollowResponse,
  UnfollowRequest: proto.user.UnfollowRequest,
  UnfollowResponse: proto.user.UnfollowResponse,
  GetUserInfoRequest: proto.user.GetUserInfoRequest,
  GetUserInfoResponse: proto.user.GetUserInfoResponse,
  GetDefaultAvatarRequest: proto.user.GetDefaultAvatarRequest,
  GetDefaultAvatarResponse: proto.user.GetDefaultAvatarResponse
};

export const chat = {
    User: proto.chat.User,
    FileStatus: proto.chat.FileStatus,
    FileInfo: proto.chat.FileInfo,
    ImageInfo: proto.chat.ImageInfo,
    Message: proto.chat.Message,
    Session: proto.chat.Session,
    Type: proto.chat.Type,
    SendACK: proto.chat.SendACK,
    ReadACK: proto.chat.ReadACK,
    WsMessage: proto.chat.WsMessage,
    GetSessionsRequest: proto.chat.GetSessionsRequest,
    GetSessionsResponse: proto.chat.GetSessionsResponse,
    GetSessionMessagesRequest: proto.chat.GetSessionMessagesRequest,
    GetSessionMessagesResponse: proto.chat.GetSessionMessagesResponse,
    LoadMoreMessagesRequest: proto.chat.LoadMoreMessagesRequest,
    LoadMoreMessagesResponse: proto.chat.LoadMoreMessagesResponse,
    GetFriendsRequest: proto.chat.GetFriendsRequest,
    GetFollowersRequest: proto.chat.GetFollowersRequest,
    GetFollowingRequest: proto.chat.GetFollowingRequest,
    GetMutualFollowersRequest: proto.chat.GetMutualFollowersRequest,
    UsersResponse: proto.chat.UsersResponse,
    UploadFileRequest: proto.chat.UploadFileRequest,
    UploadFileResponse: proto.chat.UploadFileResponse,
    DownloadFileRequest: proto.chat.DownloadFileRequest,
    DownloadFileResponse: proto.chat.DownloadFileResponse,
    GetFavoriteEmojisRequest: proto.chat.GetFavoriteEmojisRequest,
    GetFavoriteEmojisResponse: proto.chat.GetFavoriteEmojisResponse,
    AddFavoriteRequest: proto.chat.AddFavoriteRequest,
    AddFavoriteResponse: proto.chat.AddFavoriteResponse,
    GetOrCreatePrivateSessionRequest: proto.chat.GetOrCreatePrivateSessionRequest,
    GetOrCreatePrivateSessionResponse: proto.chat.GetOrCreatePrivateSessionResponse,
};
// 创建protobuf请求数据
export const createProtoRequest = (Type, data) => {
  const message = new Type();
  Object.keys(data).forEach(key => {
    const setter = `set${key.charAt(0).toUpperCase() + key.slice(1)}`;
    if (typeof message[setter] === 'function') {
      message[setter](data[key]);
    }
  });
  return message.serializeBinary();
};

// 解析protobuf响应数据
export const parseProtoResponse = (Type, buffer) => {
  const message = Type.deserializeBinary(buffer);
  if (typeof message.toObject === 'function') {
    return message.toObject();
  }
  return message;
};
